package com.project.customview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.View;

import com.project.customview.R;

public class RoundView extends View {
    private Context mContext;
    private int circleColork;
    private int arcColor;
    private int mTextColor;
    private float mTextSize;
    private String mText;
    private int startAngle;
    private int sweepAngle;
    private float mRadius;
    private int mCircleXY;
    private int mCircleXY1;
    private Paint mCirclePaint;
    private Paint mArcPaint;
    private Paint mTextPaint;
    private RectF rectF;
    private String rType;
    private static final Xfermode[] sModes = {
            new PorterDuffXfermode(PorterDuff.Mode.CLEAR),
            new PorterDuffXfermode(PorterDuff.Mode.SRC),
            new PorterDuffXfermode(PorterDuff.Mode.DST),
            new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER),
            new PorterDuffXfermode(PorterDuff.Mode.DST_OVER),
            new PorterDuffXfermode(PorterDuff.Mode.SRC_IN),
            new PorterDuffXfermode(PorterDuff.Mode.DST_IN),
            new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT),
            new PorterDuffXfermode(PorterDuff.Mode.DST_OUT),
            new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP),
            new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP),
            new PorterDuffXfermode(PorterDuff.Mode.XOR),
            new PorterDuffXfermode(PorterDuff.Mode.DARKEN),
            new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN),
            new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY),
            new PorterDuffXfermode(PorterDuff.Mode.SCREEN)
    };
    public RoundView(Context context) {
        super(context);
        mContext = context;
    }

    public RoundView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundView);
        circleColork = typedArray.getColor(R.styleable.RoundView_circleColor,Color.BLUE);
        arcColor = typedArray.getColor(R.styleable.RoundView_arcColor, Color.BLACK);
        mTextColor = typedArray.getColor(R.styleable.RoundView_rTextColor, Color.GREEN);
        mTextSize = typedArray.getDimension(R.styleable.RoundView_rTextSize, 22f);
        mText = typedArray.getString(R.styleable.RoundView_rText);
        startAngle = typedArray.getInt(R.styleable.RoundView_startAngle, 120);
        sweepAngle = typedArray.getInt(R.styleable.RoundView_sweepAngle, 100);
        mRadius = typedArray.getDimension(R.styleable.RoundView_radius,10f);
        rType = typedArray.getString(R.styleable.RoundView_rType);

        //需要资源回收
        typedArray.recycle();


    }

    public RoundView(Context context, @Nullable AttributeSet attrs,
                     int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i("aaa","widthMeasureSpec--"+widthMeasureSpec +"heightMeasureSpec--"+heightMeasureSpec);
        init(MeasureSpec.getSize(widthMeasureSpec),MeasureSpec.getSize(heightMeasureSpec));
    }
    private void init(int width,int height){
        int length = Math.min(width,height);

        mCircleXY = length/2;
        mCircleXY1 = length/4;
        //画圆
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(circleColork);
        mCirclePaint.setXfermode(sModes[4]);//或者sModes【i】
        rectF = new RectF(length*0.1f,length*0.1f,length*0.9f,length*0.9f);
       //画圆环
        mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mArcPaint.setColor(arcColor);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(width*0.1f);
        //文本
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

    }




    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSth(canvas);
    }
    private void drawSth(Canvas canvas){
        switch (rType){
            case "0":
                canvas.drawCircle(mCircleXY1,mCircleXY1,mRadius,mCirclePaint);
                canvas.drawCircle(mCircleXY,mCircleXY,mRadius,mCirclePaint);
                break;
            case "1":
                canvas.drawCircle(mCircleXY,mCircleXY,mRadius,mCirclePaint);
                break;
            case "2":
                canvas.drawCircle(mCircleXY1,mCircleXY1,mRadius,mCirclePaint);
                break;


        }

        canvas.drawArc(rectF,startAngle,sweepAngle,false,mArcPaint);
        canvas.drawText(mText,0,mText.length(),mCircleXY,mCircleXY+mTextSize/4,mTextPaint);
    }
}
